<?php
class AdminController implements ControllerInterface{
	private $view;
	private $admin;
	
	public function __construct(){
		session_start();
		$view = $this->view = new View();
	}
	
	public function IndexAction(){
		$this->isLogin();
		$view = $this->view;
		$content = array(
			'adminname'=>lib::getItem("adminname"),	
		);
		$view->display($content, "admin/index.html");
	}
	
	public function LoginAction(){
		$view = $this->view;
		if (lib::isSetItem("adminname")){
			$content = array(
				'waringTitle'=>"请勿重复登陆",
				'waringContent'=>"已经登陆，请勿重复登陆",
				'waringRoute'=>"admin/index",
			);
			$view->display($content, "standard/waring.html");
		}else {
			$content = array();
			$view->display($content, "admin/login.html"); 
		}
	}
	
	public function LoginProcessAction(){
		if (isset($_POST['adminname'])&&isset($_POST['password'])){
			$adminname = $_POST['adminname'];
			$password = $_POST['password'];
		}else {
			$route = "http://".$_SERVER['HTTP_HOST']."/admin/index";	
			header("Location:".$route);
		}
		$view = $this->view;
		$admin = new AdminModule();
		$result = $admin->isAdmin($adminname, $password);
		if ($result) {
			lib::setItem("adminname",$adminname);
			$successUrl = "http://".$_SERVER['HTTP_HOST']."/admin/index";
			header("Location:".$successUrl);
		}else{
			$content = array(
				"messageTitle" => "登陆失败",
				"messageContent" => "用户名或密码错误",
				"messageRoute" =>"admin/login",
			);
			$view->display($content, "standard/error.html");
			exit();
		}
	}
	
	public function LoginOutAction(){
		$this->isLogin();
		lib::destroyItem("adminname");
		$view = $this->view;
		$content = array(
			'successTitle'=>"退出成功",
			'successContent'=>"退出成功",
			'successRoute'=>"admin/login",
		);
		$view->display($content, "standard/success.html");
	}
	
	public function SittingAction(){
		$view = $this->view;
		$content = array(
			'adminname'=>lib::getItem("adminname"),
		);
		$view->display($content, "admin/sitting.html");
	}
	
	
	
	public function isLogin(){
		$view = $this->view;
		if(lib::isSetItem("adminname")){
				return TRUE;
			}else {
				$content = array(
					'messageTitle'=>"尚未登陆",
					'messageContent'=>"尚未登陆，无法操作",
					'messageRoute'=>"admin/login",
				);
				$view->display($content, "standard/error.html");
				exit();
			}
	}
	
}